package com.ywb.ex09.view;

import com.ywb.ex09.dao.factory.ServiceFactory;
import com.ywb.ex09.dao.impl.PetDao;
import com.ywb.ex09.dao.impl.ServiceDaoImpl;
import com.ywb.ex09.dao.service.ServiceDao;
import com.ywb.ex09.model.PetItem;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.List;

public class PetAdminDialogController extends PetAdminDialog {
    private ServiceDao service = ServiceFactory.newInstance();
    public PetAdminDialogController(){
        super();
        queryPetItem();
    }
    @Override
    public void queryPetItem(){
        String[] tablehead=new String[]{"编号","名称","价格（元）","计价单位"};
        PetDao pd=new PetDao();
        List<PetItem> list= service.queryAllData();

        String[][] tablebody = list.stream().map(pet -> {
            String[] strPet = new String[]{pet.getId(), pet.getPetname(), Double.toString(pet.getPrice()), pet.getUnit()};
            return strPet;
        }).toArray(String[][]::new);


        TableModel data=new DefaultTableModel(tablebody,tablehead);
        DefaultTableModel model = new DefaultTableModel();

        petTable.setModel(data);
    }
    @Override
    public void addPetItem(){
        String id=addIdText.getText().trim();
        String petname=addNameText.getText().trim();
        String price=addPriceText.getText().trim();
        String unit=addUnitText.getText().trim();
        PetItem petItem=new PetItem();
        petItem.setId(id);
        petItem.setPetname(petname);
        petItem.setPrice(Double.valueOf(price));
        petItem.setUnit(unit);
        boolean addSuccess= service.addPetItemforFlag(petItem);
        if(addSuccess){
            queryPetItem();//刷新表格
        }else{
            JOptionPane.showMessageDialog(this,"宠物编号不能重复，请检查宠物编号！");
        }
    }
    @Override
    public void updatePetItem(){
        String id = updateIdText.getText().trim();
        String petname=updateNameText.getText().trim();
        String price=updatePriceText.getText().trim();
        String unit=updateUnitText.getText().trim();
        PetItem petItem=new PetItem(id, petname, Double.valueOf(price), unit);
        boolean success = service.updatePetItemforFlag(petItem);
        if(success){
            queryPetItem();//刷新表格
        }else{
            JOptionPane.showMessageDialog(this,"更新失败！请重新输入宠物信息");
            updateIdText.setText("");
            updateNameText.setText("");
            updatePriceText.setText("");
            updateUnitText.setText("");
        }

    }
    @Override
    public void delPetItem(){
        String id=delIdText.getText();
        boolean delSuccess= service.deletePetItemforFlag(id);

        if(delSuccess){
            queryPetItem();//刷新表格
        }else{
            JOptionPane.showMessageDialog(this,"没有这个宠物编号，请重新输入宠物编号！");
            delIdText.getText();
        }
    }
    public static  void main(String[]args){
        new PetAdminDialogTest().setVisible(true);
    }
}
